package com.lagou.niancheng.core.config;

import com.lagou.niancheng.core.utils.ReflectionUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * com.lagou.niancheng.core.config.MethodMetaData
 *
 * @author 大犬
 * @date 2020/3/28
 */
public class MethodMetaData {
    private Method method;
    /**
     * 简单参数索引map
     */
    private Map<String, Integer> simpleParamIndexMap;
    /**
     * 是否是复杂对象的参数
     */
    private boolean isAComplexObjectParam;
    /**
     * 复杂对象类型
     */
    private Class complexObjectParamType;

    /**
     * 返回值类型
     */
    private Type genericReturnType;

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Type getGenericReturnType() {
        return genericReturnType;
    }

    public void setGenericReturnType(Type genericReturnType) {
        this.genericReturnType = genericReturnType;
    }

    public Map<String, Integer> getSimpleParamIndexMap() {
        return simpleParamIndexMap;
    }

    public void setSimpleParamIndexMap(Map<String, Integer> simpleParamIndexMap) {
        this.simpleParamIndexMap = simpleParamIndexMap;
    }

    public boolean isAComplexObjectParam() {
        return isAComplexObjectParam;
    }

    public void setAComplexObjectParam(boolean AComplexObjectParam) {
        this.isAComplexObjectParam = AComplexObjectParam;
    }

    public Class getComplexObjectParamType() {
        return complexObjectParamType;
    }

    public void setComplexObjectParamType(Class complexObjectParamType) {
        this.complexObjectParamType = complexObjectParamType;
    }

    public Object getParam(String name, Object... params) throws IllegalAccessException {
        if (isAComplexObjectParam) {
            Field field = ReflectionUtil.getField(complexObjectParamType, name);
            field.setAccessible(true);
            return field.get(params[0]);
        } else {
            Integer index = simpleParamIndexMap.get(name);
            return params[index];
        }
    }
}
